<?php
namespace app\admin\controller;

use app\common\model\ChatMsgPrivate as ChatMsgPrivateModel;
use app\service\CoachService;

class Coach extends Base
{
    /**
     * 列表
     * @return \think\response\Json
     */
    public function coach_list()
    {
        if (request()->isPost()) {
            $data = input('');
            $page = input('page', 1);
            $size = input('size', 20);
            $where = [];
            if ($data['name']) {
                $where['name'] = ['like', "%{$data['name']}%"];
            }
            $list = CoachService::S()->queryPage($where, $page, $size);
            $total = CoachService::S()->queryCount($where);
            return returnJson(200,'',compact('page','size','list','total'));
        }
    }

    /**
     * 编辑添加
     * @return \think\response\Json
     */
    public function coach_edit()
    {
        if(request()->isPost()){
            $data = input('');
            if(!$data['name']){
                return returnJson(202,'请填写姓名');
            }
            if(!$data['desc']){
                return returnJson(202,'请填写简介');
            }
            if(!$data['content']){
                return returnJson(202,'请填写教练员详情');
            }
            $save_data = [
                'name' => $data['name'],
                'desc' => $data['desc'],
                'content' => $data['content'],
                'pic' => $data['pic'],
                'update_time'=>time(),
                'ord' => $data['ord'],
                'type' => $data['type']
            ];
            if($data['id']){
                $where = [
                    'id'=>$data['id']
                ];
                $res = CoachService::S()->querySave($save_data,$where);
            }else{
                $save_data['create_time'] = time();
                $res = CoachService::S()->querySave($save_data);
            }
            if($res){
                return returnJson(200,'编辑成功');
            }else{
                return returnJson(202,'编辑失败');
            }
        }
    }


    /**
     * @return \think\response\Json
     */
    public function del_coach () {
        $input = input();
        if(!$input['id']) {
            return returnJson(1,'error');
        }
        \app\common\model\Coach::destroy(['id'=>$input['id']]);
        return returnJson(0,'删除成功');
    }

   
}

